#include "Character.h"
#include <ctime>
#include <iostream>
#include <Windows.h>

using namespace std;


Character::Character(void)
{
	character = ' ';
	for (int y = 0; y < 10; y++) {
		for (int x = 0; x < 10; x++) {
			position[x][y] = ' ';
		}
	}
}

Character::Character(char character, char matrix, int posX, int posY, char cLos)
{
	this -> character = character;
	this -> posX = posX;
	this -> posY = posY;
	this -> cLos = cLos;

	for (int y = 1; y < 10; y++) {
		for (int x = 1; x < 10; x++) {
			position[x][y] = matrix;
		}
	}
	for (int y = 1; y < 10; y++ ) {
		position[0][y] = char(0xBA);
		position[10][y] = char(0xBA);
	}
	for (int x = 1; x < 10; x++) {
		position[x][0] = char(0xCD);
		position[x][10] = char(0xCD);
	}
	position[0][0] = char(0xC9);
	position[10][0] = char(0xBB);
	position[0][10] = char(0xC8);
	position[10][10] = char(0xBC);
	
}


Character::~Character(void)
{
}

int Character::Randomizer()
{
	return rand() % 9;
}

char Character::GetChar()
{
	return character;
}

void Character::DrawDungeon()
{
	for (int y = 0; y < 11; y++) {
		for (int x = 0; x < 11; x++) {
			cout << position[x][y];
		}
		cout << endl;
	}
}
void Character::SetPosition(int x, int y, char object)
{
	position[x][y] = object;
}

void Character::MoveCharacter(string input)
{
	

	if (input == "down" || input == "d") {
		if (posY == 9) {
			cout << "You can't move that way!";
			Sleep(1000);
		}

		else
		{
			
			posY += 1;
			SetPosition(posX, posY, character);
			SetLineOfSight(posX, posY );
		}
	}
	else if (input == "up" || input == "u") {
		if (posY == 1) {
			cout << "You can't move that way!";
			Sleep(1000);
		}
		else {
			
			posY -= 1;
			SetPosition(posX, posY, character);
			SetLineOfSight(posX, posY );
		}
	}
	else if (input == "left" || input == "l") {
		if (posX == 1) {
			cout << "You can't move that way!";
			Sleep(1000);
		}
		else {
			
			posX -= 1;
			SetPosition(posX, posY, character);
			SetLineOfSight(posX , posY);
		}
	}
	else if (input == "right" || input == "r") {
		if (posX == 9) {
			cout << "You can't move that way!";
			Sleep(1000);
		}
		else {
			
			
			posX += 1;
			SetPosition(posX, posY, character);
			SetLineOfSight(posX , posY);
		}

	}
}

int Character::GetposX()
{
	return posX;
}
int Character::GetposY()
{
	return posY;
}

void Character::SetLineOfSight(int posX, int posY) 
{
	if (position[posX + 1][posY] == char(0xB2) || position[posX + 1][posY] == char(0x01)) {
		SetPosition(posX + 1, posY, cLos);
		
		if (position[posX ][posY +1] == char(0xB0)|| position[posX + 2][posY] == char(0x01)) {
			SetPosition(posX , posY +1, char(0xB2));
		}

	}
	if (position[posX - 1][posY] == char(0xB2)|| position[posX - 1][posY] == char(0x01)) {
		SetPosition(posX - 1, posY, cLos);
		if (position[posX ][posY -1] == char(0xB0)|| position[posX - 2][posY] == char(0x01)) {
			SetPosition(posX , posY -1, char(0xB2));
		}
	}
	if (position[posX][posY + 1] == char(0xB2) || position[posX][posY + 1] == char(0x01)) {
		SetPosition(posX, posY + 1, cLos);
		if (position[posX ][posY + 2] == char(0xB0)|| position[posX][posY + 2] == char(0x01)) {
			SetPosition(posX , posY + 2, char(0xB2));
		}
	}
	if (position[posX][posY - 1] == char() || position[posX][posY - 1] == char(0x01)) {
		SetPosition(posX, posY - 1, cLos);
		if (position[posX ][posY - 2] == char(0xB0) || position[posX][posY - 2] == char(0x01)) {
			SetPosition(posX , posY - 2, char(0xB2));
		}
	}
	
} 